package com.gxa.ehome.proxy;

import com.gxa.ehome.service.SysUserService;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Component("methodInterceptor")
public class SysUserServiceMethodInterceptor implements MethodInterceptor {

    private SysUserService sysUserService = null;

    public SysUserServiceMethodInterceptor(SysUserService sysUserService){
        this.sysUserService = sysUserService;
    }

    // 需要实现拦截方法
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        // 操作前的记录: 执行的方法:methodName,传递的参数是:[1,2,3]
        System.out.println("执行的方法:"+method.getName()+",传递的参数是:"+ Arrays.toString(objects));
        Object invoke = method.invoke(sysUserService, objects);
        //操作后的记录
        System.out.println("方法成功执行,返回的结果是:"+invoke);
        return invoke;
    }
}
